<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
</head>
<body>
<div id="app">
    <!--action用来设置图片上传的地址
        name用来设置上传时的参数名
        limit设置图片数量
    -->
    <el-upload
            limit="1"
            name="pic"
            action="/upload"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-success="handleSuccess"
            :on-remove="handleRemove">
        <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
    <hr>
    <input type="text" v-model="image.intro" placeholder="说点儿啥...">
    <input type="button" value="发布" @click="send()">
    <img :src="image.url" width="50%" alt="">
</div>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<script>
   let v = new Vue({
        el: '#app',
        data: function() {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                image:{intro:"",url:""},


            }
        }, methods: {
           handleRemove(file, fileList) {
               console.log(file, fileList);
              //得到删除的文件路径
              let url = file.response;
              axios.get("/remove?url="+url).then(function (response) {
                  console.log("服务器删除成功!");
              })

           },
           handlePictureCardPreview(file) {
               this.dialogImageUrl = file.url;
               this.dialogVisible = true;
           },
           handleSuccess(response,file,fileList){
                console.log("response="+response);
                console.log(file);
                console.log(fileList);
                v.image.url = response;
           },
           send(){
               if (v.image.url==""){
                   alert("请选择上传的图片!");
                   return;
               }
               //发出异步请求
               axios.post("/insert",v.image).then(function () {
                   alert("发布完成!")
                   location.href = "/";
               })
           }
       }
    })
</script>
</html>